package cn.com.homeyoda.aimate.aihealth.ui.health;

import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzq.mvvmsmart.utils.GlideLoadUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.ChronicDiseaseItemBean;
import cn.com.homeyoda.aimate.aihealth.bean.ChronicDiseaseSelectBean;
import cn.com.homeyoda.aimate.aihealth.bean.FamilyMembersBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityChronicDiseaseAddBinding;
import cn.com.homeyoda.aimate.aihealth.manager.UserInfoManager;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.DiseaseSelectAdapter;
import cn.com.homeyoda.aimate.aihealth.ui.health.viewModel.ChronicDiseaseViewMode;
import cn.com.homeyoda.aimate.aihealth.views.MemberChangeDialog;
import cn.com.homeyoda.aimate.aihealth.views.SpaceItemDecoration;

/**
 * 添加慢性病
 */
public class ChronicDiseaseAddActivity extends BaseActivity<ActivityChronicDiseaseAddBinding, ChronicDiseaseViewMode> {

    DiseaseSelectAdapter selectAdapter;
    MemberChangeDialog memberChangeDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public ChronicDiseaseViewMode initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(ChronicDiseaseViewMode.class);
    }

    @Override
    public void initData() {
        super.initData();

        initView();
        viewModel.initUserInfo();
    }

    private void initView() {

        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        binding.tvTitle.setText(R.string.chronic_disease);

        initRecyclerView();

        binding.btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String dictCodes = "";
                for (ChronicDiseaseItemBean.DataBean bean : viewModel.chroincDiseaseSelectList.getValue()) {
                    if (bean.isSelect()) {
                        UserInfoManager.getInstance().chronicDiseaseMap.put(bean.getDictCode(), bean);
                        dictCodes += bean.getDictCode()+",";
                    } else {
                        if (UserInfoManager.getInstance().chronicDiseaseMap.containsKey(bean.getDictCode())) {
                            UserInfoManager.getInstance().chronicDiseaseMap.remove(bean.getDictCode());
                        }
                    }
                }

                viewModel.attentionChronicAdd(dictCodes);
                startActivity(ChronicDiseaseActivity.class);
                finish();
            }
        });

        binding.btnChangeMember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewModel.planUserStateLiveData.getValue() == null) {
                    ToastUtils.showLong("成员未初始化，请稍后重试");
                    return;
                }

                if (memberChangeDialog == null) {
                    memberChangeDialog = new MemberChangeDialog(ChronicDiseaseAddActivity.this, viewModel.familyMembers, viewModel.planUserStateLiveData.getValue().getId()
                            , new MemberChangeDialog.SelectClickListener() {
                        @Override
                        public void onSelectMember(FamilyMembersBean.DataBean.FamilyMembers member) {
                            viewModel.changeMember(member);
                        }
                    });
                } else {
                    memberChangeDialog.setDatas(viewModel.familyMembers, viewModel.planUserStateLiveData.getValue().getId());
                }

                memberChangeDialog.show();
            }
        });

    }


    private void initRecyclerView() {
        selectAdapter = new DiseaseSelectAdapter(new ArrayList<>(), this);
        binding.rvDiseaseSelect.setLayoutManager(new GridLayoutManager(this, 3));
        binding.rvDiseaseSelect.addItemDecoration(new SpaceItemDecoration(0, 0, 25, 20));
        binding.rvDiseaseSelect.setAdapter(selectAdapter);

        selectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ChronicDiseaseItemBean.DataBean bean = selectAdapter.getData().get(position);
                bean.setSelect(!bean.isSelect());
                selectAdapter.notifyDataSetChanged();

            }
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.planUserStateLiveData.observe(this, userBean -> {
            binding.setPlanUserBean(userBean);
            GlideLoadUtils.loadImg(binding.crvHead, userBean.getAvatar(), R.mipmap.ic_member1);
            viewModel.initSelectData();
        });

        viewModel.chroincDiseaseSelectList.observe(this, chroincDiseaseSelectList -> {
            if (selectAdapter != null) {
                selectAdapter.setNewData(chroincDiseaseSelectList);
            }
        });

    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_chronic_disease_add;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


}
